package com.ruoyi.bantong.controller;

import java.util.List;

import com.ruoyi.common.exception.CustomException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bantong.domain.BtRedbagPoll;
import com.ruoyi.bantong.service.IBtRedbagPollService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 红包玩法池Controller
 * 
 * @author laohei
 * @date 2021-02-06
 */
@RestController
@RequestMapping("/bantong/redbagpoll")
public class BtRedbagPollController extends BaseController
{
    @Autowired
    private IBtRedbagPollService btRedbagPollService;

    /**
     * 查询红包玩法池列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:poll:list')")
    @GetMapping("/list")
    public TableDataInfo list(BtRedbagPoll btRedbagPoll)
    {
        startPage();
        List<BtRedbagPoll> list = btRedbagPollService.selectBtRedbagPollList(btRedbagPoll);
        return getDataTable(list);
    }

    /**
     * 导出红包玩法池列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:poll:export')")
    @Log(title = "红包玩法池", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BtRedbagPoll btRedbagPoll)
    {
        List<BtRedbagPoll> list = btRedbagPollService.selectBtRedbagPollList(btRedbagPoll);
        ExcelUtil<BtRedbagPoll> util = new ExcelUtil<BtRedbagPoll>(BtRedbagPoll.class);
        return util.exportExcel(list, "poll");
    }

    /**
     * 获取红包玩法池详细信息
     */
    //@PreAuthorize("@ss.hasPermi('bantong:poll:query')")
    @GetMapping(value = "/{rpId}")
    public AjaxResult getInfo(@PathVariable("rpId") Integer rpId)
    {
        return AjaxResult.success(btRedbagPollService.selectBtRedbagPollById(rpId));
    }

    /**
     * 新增红包玩法池
     */
    //@PreAuthorize("@ss.hasPermi('bantong:poll:add')")
    @Log(title = "红包玩法池", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BtRedbagPoll btRedbagPoll)
    {
        return toAjax(btRedbagPollService.insertBtRedbagPoll(btRedbagPoll));
    }

    /**
     * 新增红包玩法池
     */
    //@PreAuthorize("@ss.hasPermi('bantong:poll:add')")
    @Log(title = "红包玩法池", businessType = BusinessType.INSERT)
    @PostMapping("/addPoll")
    public AjaxResult addPoll()
    {
        return toAjax(btRedbagPollService.addBtRedbagPoll());
    }

    /**
     * 修改红包玩法池
     */
    //@PreAuthorize("@ss.hasPermi('bantong:poll:edit')")
    @Log(title = "红包玩法池", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BtRedbagPoll btRedbagPoll)
    {
        int rows = 0;
        try {
            rows = btRedbagPollService.updateBtRedbagPoll(btRedbagPoll);
            return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof CustomException){
                return AjaxResult.error(e.getMessage());
            }else {
                return AjaxResult.error();
            }
        }
    }

    /**
     * 删除红包玩法池
     */
    //@PreAuthorize("@ss.hasPermi('bantong:poll:remove')")
    @Log(title = "红包玩法池", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rpIds}")
    public AjaxResult remove(@PathVariable Integer[] rpIds)
    {
        return toAjax(btRedbagPollService.deleteBtRedbagPollByIds(rpIds));
    }

    /**
     * 提前开奖
     */
    @Log(title = "红包玩法池", businessType = BusinessType.UPDATE)
    @PutMapping("/publishTimeNow/{rpId}")
    public AjaxResult updateBtRedPoolPublishTime(@PathVariable Integer rpId)
    {
        return toAjax(btRedbagPollService.updateBtRedPoolPublishTime(rpId));
    }

    /**
     * 红包玩法池  状态
     *
     * @return 结果
     */
    @GetMapping("/getRedbagStatus")
    public AjaxResult getRedbagStatus()
    {
        return AjaxResult.success(btRedbagPollService.getRedbagStatus());
    }
}
